package com.ityueqiangu.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.ityueqiangu.core.constant.Constants;
import com.ityueqiangu.core.exception.BizException;
import com.ityueqiangu.system.domain.SysUser;
import com.ityueqiangu.system.mapper.SysUserMapper;
import com.ityueqiangu.system.service.ISysUserService;
import com.ityueqiangu.core.util.StringUtils;
import com.ityueqiangu.core.enums.CommonEnum;
import com.ityueqiangu.core.web.result.ResultDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author Clever、xia
 * @title: SysUserServiceImpl
 * @description:
 * @date 2021-03-14 07:59:16
 */
@Service
public class SysUserServiceImpl implements ISysUserService{

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询分页记录
     *
     * @return 返回集合，没有返回空List
     */
     public List<SysUser> selectSysUserList(SysUser sysUser) {
       return sysUserMapper.selectSysUserList(sysUser);
    }


    /**
     * 根据主键查询
     *
     * @param id 主键
     * @return 返回ResultInfo
     */
    public SysUser selectSysUserById(Integer id) {
    	return sysUserMapper.selectSysUserById(id);
    }
	
    /**
     * 新增
     *
     * @param sysUser 新增的记录
     * @return 返回ResultInfo
     */
    public Integer insertSysUser(SysUser sysUser) {
        //判断登录名称
        //更具loginName查询记录是否存在
        SysUser sysUserParam = new SysUser();
        String loginName = PinyinUtil.getPinyin(sysUser.getUserName(),"");
        sysUserParam.setLoginName(loginName);
        SysUser sysUserResult = sysUserMapper.selectSysUserByLoginName(sysUserParam);
        if (ObjectUtil.isNotNull(sysUserResult)) {
            loginName = PinyinUtil.getPinyin(sysUserResult.getUserName()+sysUserResult.getId(),"");
        }
        sysUser.setLoginName(loginName);
        if (StringUtils.isBlank(sysUser.getRoleName())) {
            sysUser.setRoleName("普通用户");
        }
        sysUser.setCreateType("0");
        sysUser.setPassword("123456");
    	return sysUserMapper.insertSysUser(sysUser);
    }


    /**
     * 注册
     *
     * @param sysUser 新增的记录
     * @return 返回ResultInfo
     */
    public String register(SysUser sysUser) {
        //判断登录名称
        //更具loginName查询记录是否存在
        SysUser sysUserParam = new SysUser();
        String loginName = PinyinUtil.getPinyin(sysUser.getUserName(),"");
        sysUserParam.setLoginName(loginName);
        SysUser sysUserResult = sysUserMapper.selectSysUserByLoginName(sysUserParam);
        if (ObjectUtil.isNotNull(sysUserResult)) {
            loginName = PinyinUtil.getPinyin(sysUserResult.getUserName()+sysUserResult.getId(),"");
        }
        sysUser.setLoginName(loginName);
        if (StringUtils.isBlank(sysUser.getRoleName())) {
            sysUser.setRoleId(2);
            sysUser.setRoleName("普通用户");
        }
        sysUser.setCreateType("1");
        sysUser.setPassword("123456");
        Integer result = sysUserMapper.insertSysUser(sysUser);
        if (ObjectUtil.isNull(result)||result< Constants.ONE) {
            throw new BizException(CommonEnum.REGISTER_FAILURE);
        }
        return loginName;
    }
	
	
    /**
     * 修改
     *
     * @param sysUser 修改的记录
     * @return 返回
     */
    public Integer updateSysUser(SysUser sysUser) {
    	return sysUserMapper.updateSysUser(sysUser);
    }
	
  
    /**
     * 删除记录
     *
     * @param id 主键
     * @return 返回
     */
    public Integer deleteSysUserById(Integer id) {
    	return sysUserMapper.deleteSysUserById(id);
    }

    /**
     * 根据登录名 进行查询
     * @param sysUser
     * @return
     */
    @Override
    public SysUser selectSysUserByLoginName(SysUser sysUser) {

        return sysUserMapper.selectSysUserByLoginName(sysUser);
    }

}